import Vue from "vue";
import VueRouter from "vue-router";

import Index from "../components/index/index_container.vue";
import Login from "../components/index/login.vue";
import Register from "../components/index/register.vue";

import Admin from "../components/admin/admin_container.vue";
import Administrator from "../components/admin/administrator.vue";
import ScneicSpot from "../components/admin/scenic_spot.vue";
import Hotel from "../components/admin/hotel.vue";
import TravelAgency from "../components/admin/travel_agency.vue";
import Car from "../components/admin/car.vue";
import WarningMsg from "../components/admin/warning_msg.vue";
import TourGuideApplication from "../components/admin/tour_guide_application.vue";
import OnDutyTourGuide from "../components/admin/on_duty_tour_guide.vue";
import Complaints from "../components/admin/complaints.vue";

import User from "../components/normal_user/normal_user_container.vue";
import UserIndex from "../components/normal_user/index/index_container.vue";
import Hot from "../components/normal_user/index/hot.vue";
import MoreEmterprise from "../components/reusable_components/more_enterprise.vue";
import BookTrip from "../components/normal_user/book_trip/book_trip.vue";
import MyAppointment from "../components/normal_user/my_appointment.vue";
import UserComplaints from "../components/normal_user/complaints/complaints.vue";
import MyComplaints from "../components/normal_user/complaints/my_complaints.vue";
import PersonalCenter from "../components/normal_user/personal_center.vue";
import UserGuide from "../components/guide/guide.vue";
import ItineraryList from "../components/guide/itinerary_list.vue";
import MemberList from "../components/guide/member_list.vue";
import ToBeGuide from "../components//guide/to_be_guide/to_be_guide.vue";
import Notice from "../components/normal_user/index/notice.vue";
import EnterPriseDetail from "../components/reusable_components/enterprise_detail.vue";
import ItineraryDetail from "@/components/normal_user/book_trip/itinerary_detail.vue";

import NotFound from "@/components/system/404.vue";
import Forbidden from "@/components/system/403.vue";
import InternalError from "@/components/system/500.vue";
import NetworkError from "@/components/system/network_error.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "*",
    redirect: "/404",
  },
  {
    path: "/404",
    name: "not_found",
    component: NotFound,
  },

  {
    path: "/403",
    name: "forbidden",
    component: Forbidden,
  },
  {
    path: "/500",
    name: "internal_error",
    component: InternalError,
  },
  {
    path: "/network_error",
    name: "network_error",
    component: NetworkError,
  },

  {
    path: "/",
    name: "root",
    redirect: "/index",
  },

  // 登陆注册

  {
    path: "/index",
    name: "index",
    component: Index,
    redirect: "/login",
    children: [
      { path: "/login", component: Login },
      { path: "/register", component: Register },
    ],
    meta: {
      is_login: true,
      // 不允许后退
      allowBack: false,
    },
  },

  // 管理员页面
  {
    path: "/admin",
    name: "admin",
    component: Admin,
    redirect: "/admin/scneic_spot",
    children: [
      { path: "/admin/scneic_spot", component: ScneicSpot }, //星级景区
      { path: "/admin/hotel", component: Hotel }, //星级酒店
      { path: "/admin/travel_agency", component: TravelAgency }, //旅行社
      { path: "/admin/administrator", component: Administrator }, //企业人员
      { path: "/admin/car", component: Car }, //救援车辆
      { path: "/admin/warning_msg", component: WarningMsg }, //告警信息
      //导游申请
      {
        path: "/admin/tour_guide_application",
        component: TourGuideApplication,
      },
      // 在岗导游
      { path: "/admin/on_duty_tour_guide", component: OnDutyTourGuide },
      // 投诉&建议
      { path: "/admin/complaints", component: Complaints },
    ],
  },

  // 普通用户、导游页面
  {
    path: "/user",
    name: "user",
    component: User,
    redirect: "/user/index",
    //首页
    children: [
      {
        path: "/user/index",
        component: UserIndex,
        redirect: "/user/index/hot",
        children: [
          { path: "/user/index/hot", component: Hot },
          { path: "/user/index/hot/notice", component: Notice },
        ],
      },
      { path: "/user/book_trip", component: BookTrip }, //预约旅行
      { path: "/user/my_appointment", component: MyAppointment }, //我的预约
      { path: "/user/complaints", component: UserComplaints }, //投诉
      { path: "/user/personal_center", component: PersonalCenter }, //个人中心
      {
        path: "/user/guide",
        name: "user_guide",
        component: UserGuide,
      }, //导游
      { path: "/user/guide/itinerary_list", component: ItineraryList }, //导游行程单
      { path: "/user/guide/member_list", component: MemberList }, //团成员
      { path: "/user/to_be_guide", component: ToBeGuide }, //导游申请
      { path: "/user/enterprise_detail", component: EnterPriseDetail }, // 企业详情
      { path: "/user/more_enterprise", component: MoreEmterprise }, // 更多企业
      { path: "/user/itinerary_detail", component: ItineraryDetail }, // 旅行单详情
      { path: "/user/my_complaints", component: MyComplaints }, // 我的投诉
    ],
  },
];

let originPush = VueRouter.prototype.push; //备份原push方法

VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    //如果传了回调函数，直接使用
    originPush.call(this, location, resolve, reject);
  } else {
    //如果没有传回调函数，手动添加
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

const router = new VueRouter({
  routes,
});

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // to 目标
  // from 来源
  // 去登陆注册页,直接放行

  let allowBack = true; //  默认允许回退

  // 定义了allowBack的
  if (to.meta.allowBack !== undefined) {
    allowBack = to.meta.allowBack;
  }

  // 不允许回退
  if (!allowBack) {
    history.pushState(null, null, location.href);
  }

  if (to.path === "/index" || to.path === "/login" || to.path === "/register") {
    sessionStorage.removeItem("user_id");
    sessionStorage.removeItem("token");
    sessionStorage.removeItem("type");
    return next();
  }

  if (to.path === "/admin") {
    if (sessionStorage.type !== "1") return next("index");
  }

  // 没有token，且不是去登陆注册页，拦下来
  if (
    !sessionStorage.token &&
    to.path !== "/login" &&
    to.path !== "/register"
  ) {
    return next("/index");
  }

  next();
});

export default router;
